﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using QuanLyDuAn.DataAccess;

namespace QuanLyDuAn.GUI
{
    public partial class FrmApplicant : DevComponents.DotNetBar.Office2007Form
    {
        public int permission;
        DataSet ds;
        ApplicantController applicantcontrol = new ApplicantController();
        public FrmApplicant()
        {
            InitializeComponent();
        }
        public void Permission()
        {
            btnAdd.Enabled = false;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }
        public void ResetForm()
        {
            txtID.Text = "";
            txtName.Text = "";
            txtPhone.Text = "";
            //txtQualification.Text = "";
            dateTimeInput1.Text = "";
            dateTimeInput2.Text = "";
            txtExperience.Text = "";
         }
        public void LoadForm()
        {
            ds = applicantcontrol.LoadApplicant();
            dataGridViewX1.DataSource = ds.Tables[0];
        }

        public int CheckValue()
        {
            int check = 0;
            int num;
            String a = txtExperience.Text.ToString();
            bool isNumber = int.TryParse(a, out num);
            if (isNumber && a.CompareTo("") != 0)
            {
                check = 1;
            }
            else
            {
                check = 0;
                MessageBox.Show("Applicant experience is a Number !");
            }
            return check;
        }

        public bool CheckExist()
        {
            bool check;
            String iId = txtID.Text;
            DataTable dt = new DataTable();
            ds = new DataSet();
            ds = applicantcontrol.CheckApplicant(iId);
            dt = ds.Tables[0];
            if (dt.Rows.Count != 0)
            {
                check = false;
                MessageBox.Show("Applicant is Exits !");
            }
            else
            {
                check = true;
            }
            return check;
        }
        public String SelectStatus()
        {
            String status;
            if (rdopen.Checked == true)
            {
                status = "Open";
            }
            else if (rdclose.Checked == true)
            {
                status = "Close";
            }
            else
            {
                status = "Inpocess";
            }
            return status;
        }
        
        public void InsertApplicant()
        {
            String id = txtID.Text;
            String date = dateTimeInput1.Value.ToString();
            String status = this.SelectStatus();
            String name = txtName.Text;
            String datebirth = dateTimeInput2.Value.ToString();
            String phone = txtPhone.Text;
            String qualification = (String)cmbperiod.SelectedItem;
            String a = txtExperience.Text;
            if (this.CheckValue() == 1)
            {
                int experience = Int32.Parse(a);
                applicantcontrol.InsertApplicant(id, date, status, name, datebirth, phone, qualification, experience);
                MessageBox.Show("Insert successful !!!");
                this.LoadForm();
                this.ResetForm();
            }
        }
        public void UpdateApplicant()
        {
            String id = txtID.Text;
            String date = dateTimeInput1.Value.ToString();
            String status = this.SelectStatus();
            String name = txtName.Text;
            String datebirth = dateTimeInput2.Value.ToString();
            String phone = txtPhone.Text;
            String qualification = (String)cmbperiod.SelectedItem;
            String a = txtExperience.Text;
            if (this.CheckValue() == 1)
            {
                int experience = Int32.Parse(a);
                applicantcontrol.UpdateApplicant(id, date, status, name, datebirth, phone, qualification, experience);
                MessageBox.Show("Update successful !!!");
                this.LoadForm();
                this.ResetForm();
            }
        }
        public bool checkEmpty()
        {
            bool check;
            if (txtID.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Applican ID must be input");
            }
            else if (txtName.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Name is must be input");
            }
            else if (txtExperience.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Experience is must be input");
            }
            else if (txtPhone.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Phone is must be input");
            }
            else if (cmbperiod.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("period is must be input");
            }
            else if (dateTimeInput1.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please Select Date ");
            }
            else if (dateTimeInput2.Text.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please Select DateLine ");
            }
            else
            {
                check = true;

            }
            return check;

        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if ((this.checkEmpty() == true)&& (this.CheckExist()==true))
            {
                this.InsertApplicant();
            }
        }

        private void FrmApplicant_Load(object sender, EventArgs e)
        {
            cmbperiod.Items.Add("PHP");
            cmbperiod.Items.Add("Android");
            cmbperiod.Items.Add("C#.net");
            cmbperiod.Items.Add("Java");
            cmbperiod.Items.Add("Asp");
            cmbperiod.SelectedItem = "PHP";
            rdopen.Checked = true;
            this.LoadForm();
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.UpdateApplicant();
            this.LoadForm();
            this.ResetForm();
        }

        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if(permission ==1)
            {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridViewX1.Rows[e.RowIndex];
            txtID.Text = row.Cells[0].Value.ToString();
            txtName.Text = row.Cells[3].Value.ToString();
            txtPhone.Text = row.Cells[5].Value.ToString();
            //txtQualification.Text = row.Cells[6].Value.ToString();
            txtExperience.Text = row.Cells[7].Value.ToString();
            dateTimeInput1.Text=row.Cells[1].Value.ToString();
            dateTimeInput2.Text = row.Cells[4].Value.ToString();
            String status = row.Cells[2].Value.ToString();
            if (status.CompareTo("Open") == 0)
            {
                rdopen.Checked = true;
            }
            else if (status.CompareTo("Close") == 0)
            {
                rdclose.Checked = true;
            }
            else
            {
                rdinprocess.Checked = true;
            }
            }
            else
            {
                MessageBox.Show("You don't have permisson to update information");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            {
                if (MessageBox.Show("You really want to delete?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        String id = txtID.Text;
                        applicantcontrol.DelApplicant(id);
                        MessageBox.Show("Del successfull", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadForm();
                        ResetForm();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Errors");
                    }
                }
            }
        }     
    }
}
